package com.prince.web.controller;

import com.prince.aspect.AppLog;
import com.prince.entity.InventoryVo;
import com.prince.enums.DirectionEnum;
import com.prince.enums.GiftEnum;
import com.prince.enums.ShopStatusEnum;
import com.prince.model.Inventory;
import com.prince.model.SalesRecord;
import com.prince.page.Page;
import com.prince.util.PoiExcelExportUtil;
import com.prince.web.base.BaseController;
import com.prince.web.base.SessionUtil;
import com.prince.web.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 功能描述: 库存表
 * @Author: 王培喜 58528792@163.com
 * @Date: 2021/4/6 14:33
 */
@Controller
public class InventoryController extends BaseController {

    @Autowired
    private InventoryService inventoryService;

    /**
     * 功能描述: 分页查询库存
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 15:25
     */
    @AppLog
    @PostMapping("/findInventoryByPage")
    public ResponseEntity<Map<String,Object>> findInventoryByPage(@RequestParam Map<String,Object> params) {
        Page page = this.getRequestPageRollInfo(params);
        Map<String,Object> map = inventoryService.findInventoryByPage(params,page);
        return new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
    }
    /**
     * 功能描述: 导出库存
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @GetMapping("/exportInventory")
    public void exportInventory(@RequestParam Map<String,Object> params, HttpServletResponse response){
        Map<String,Object> map = inventoryService.findInventoryByPage(params,null);
        Object object = map.get("rows");
        if(null == object) {
            return;
        }
        List<InventoryVo> list = (List<InventoryVo>)object;
        list.forEach(x->{
            x.setRemark(ShopStatusEnum.getName(x.getDirection()));
        });
        //导出
        Date date = new Date();
        PoiExcelExportUtil poiExcelExportUtil = new PoiExcelExportUtil();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        String strDate = simpleDateFormat.format(date);
        String[] titleColumns = {"name","supplier", "productUnit","specifications","purchasePrice","remark","number","firstEnterDate","lastLeaveDate","updater","created"};
        String[] titleNames = {"商品名称","供应商","单位", "规格","零售价","状态","数量","第一次入库时间","最后一次出库时间","操作人","时间"};
        //生成下载流
        poiExcelExportUtil.writeExcel(list, titleColumns, titleNames, response, "库存-" + strDate);

    }
    /**
     * 功能描述: 添加库存
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/addInventoryNumber")
    public ResponseEntity<Map<String,Object>> addInventoryNumber(Integer id, BigDecimal number) {
        String name = SessionUtil.getSession().getLoginName();
        Map<String,Object> map = inventoryService.addInventoryNumber(id,number,name);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }

    /**
     * 功能描述: 库存退货
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/inventoryReturnNumber")
    public ResponseEntity<Map<String,Object>> inventoryReturnNumber(Integer id,BigDecimal number) {
        String name = SessionUtil.getSession().getLoginName();
        Map<String,Object> map = inventoryService.inventoryReturnNumber(id, number, name);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    /**
     * 功能描述: 库存退货
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/updateInventoryStatus")
    public ResponseEntity<Map<String,Object>> updateInventoryStatus(Inventory inventory) {
        String name = SessionUtil.getSession().getLoginName();
        inventory.setUpdater(name);
        Map<String,Object> map = inventoryService.updateInventoryStatus(inventory);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    /**
     * 功能描述: 采购记录
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 15:25
     */
    @AppLog
    @PostMapping("/findProcurementByPage")
    public ResponseEntity<Map<String,Object>> findProcurementByPage(@RequestParam Map<String,Object> params) {
        Page page = this.getRequestPageRollInfo(params);
        Map<String,Object> map = inventoryService.findProcurementByPage(params,page);
        return new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
    }
    /**
     * 功能描述: 导出采购记录
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @GetMapping("/exportProcurement")
    public void exportProcurement(@RequestParam Map<String,Object> params, HttpServletResponse response){
        Map<String,Object> map = inventoryService.findProcurementByPage(params,null);
        Object object = map.get("rows");
        if(null == object) {
            return;
        }
        List<InventoryVo> list = (List<InventoryVo>)object;
        list.forEach(x->{
            x.setRemark(DirectionEnum.getName(x.getDirection()));
        });
        //导出
        Date date = new Date();
        PoiExcelExportUtil poiExcelExportUtil = new PoiExcelExportUtil();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        String strDate = simpleDateFormat.format(date);
        String[] titleColumns = {"name","supplier", "productUnit","specifications","remark","number","updater","created"};
        String[] titleNames = {"商品名称","供应商","单位", "规格","状态","数量","操作人","时间"};
        //生成下载流
        poiExcelExportUtil.writeExcel(list, titleColumns, titleNames, response, "采购记录-" + strDate);

    }
}
